package org.example.tinyurl.controller;

import org.apache.dubbo.config.annotation.DubboReference;
import org.example.tinyurl.api.TinyUrlService;
import org.example.tinyurl.entity.RpcResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author: zk
 * @date: 2023/6/22 11:19
 */
@RestController
@RequestMapping("/tinyurl")
public class TinyUrlController {

    @DubboReference
    private TinyUrlService tinyUrlService;

    /**
     * 由长链接生成短链接
     *
     * @param longUrl
     * @return
     */
    @PostMapping("/getTinyUrl")
    public RpcResult<String> getTinyUrl(String longUrl) {
        return tinyUrlService.generateTinyUrl(longUrl);
    }

    /**
     * 由短链接得到长链接
     *
     * @param tinyUrl
     * @return
     */
    @GetMapping("/getLongUrl")
    public RpcResult<String> getLongUrl(String tinyUrl) {
        return tinyUrlService.getLongUrl(tinyUrl);
    }
}
